package com.hmys.api.controller;

import com.github.pagehelper.PageInfo;
import com.hmys.api.model.*;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.response.ResultPage;
import com.hmys.api.service.*;
import com.hmys.api.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * Created by chen365 on 2018/03/26.
 */
@RestController
@Api(tags = "城市")
@RequestMapping(value = "/city")
public class CityController {

    @Autowired
    private WebCityService webCityService;

    @Autowired
    private WebCityIndexService webCityIndexService;

    @Autowired
    private WebCityPhotoService webCityPhotoService;

    @Autowired
    private WebCityTemperatureService webCityTemperatureService;

    @Autowired
    private WebHousePriceRentTrendService webHousePriceRentTrendService;

    @ApiOperation(value = "列表", notes = "列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageIndex", value = "页码", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页数", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "Long", name = "countryId", value = "国家ID", required = false, paramType = "query")
    })
    @GetMapping(value = "/list/{pageIndex}/{pageSize}")
    public Object list(@PathVariable("pageIndex") Integer pageIndex,
                       @PathVariable("pageSize") Integer pageSize,
                       @RequestParam(value = "countryId", required = false, defaultValue = "1") Long countryId) {

        System.err.println("countryId:"+countryId);
        WebCity record = new WebCity();
        record.setCountryId(countryId);
        record.setSort(1);
        record.setStatus(true);
        PageInfo<WebCity> pageInfo = webCityService.selectByBean(pageIndex, pageSize, record);
        if (pageInfo.getList().size() > 0) {
            return new ResultPage(ResultCode.SUCCESS, pageInfo);
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "详情", notes = "详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "城市ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/detail/{id}")
    public Object detail(@PathVariable("id") Long id) {
        if (!StringUtils.isEmpty(id) && id < 1) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        WebCityWithBLOBs record = webCityService.selectByPrimaryKey(id);
        if (record != null) {
            if (record.getStatus()) {
                return new Result<>(ResultCode.SUCCESS, record);
            } else {
                return new Result<>(ResultCode.HAVE_DELETED);
            }
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }


    @ApiOperation(value = "相册", notes = "相册", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "城市ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/photo/{id}")
    public Object index(@PathVariable("id") Long id) {
        if (!StringUtils.isEmpty(id) && id < 1) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        List<WebCityPhoto> list = webCityPhotoService.selectByCityId(id);
        if (list.size() > 0) {
            return new Result<>(ResultCode.SUCCESS, list);
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "指数", notes = "指数", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "城市ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/index/{id}")
    public Object photo(@PathVariable("id") Long id) {
        if (!StringUtils.isEmpty(id) && id < 1) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        WebCityIndex record = webCityIndexService.selectByCityId(id);
        if (record != null) {
            return new Result<>(ResultCode.SUCCESS, record);
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "气温", notes = "气温", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "城市ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/temperature/{id}")
    public Object temperature(@PathVariable("id") Long id) {
        if (StringUtils.isEmpty(id) || id < 1) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        List<WebCityTemperature> list = webCityTemperatureService.selectByCityId(id);
        if (list.size() > 0) {
            return new Result<>(ResultCode.SUCCESS, list);
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "房价与租金走势", notes = "房价与租金走势", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "cityId", value = "城市ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "type", value = "类型，1房价，2租金", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "btime", value = "开始日期", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "etime", value = "结束日期", required = false, paramType = "query")
    })
    @GetMapping(value = "/trend/{cityId}/{type}")
    public Object trend(@PathVariable("cityId") Long cityId, @PathVariable("type") Integer type, String btime, String etime) {
        WebHousePriceRentTrend record = new WebHousePriceRentTrend();
        if (StringUtils.isEmpty(cityId)) {
            return new Result(ResultCode.ERROR_PARAM);
        }
        if (StringUtils.isEmpty(type)) {
            return new Result(ResultCode.ERROR_PARAM);
        }
        record.setCityId(cityId);
        record.setType(type);
        if (!StringUtils.isEmpty(btime) && !StringUtils.isEmpty(etime)) {
            if (DateUtils.getDateByStr(btime).compareTo(DateUtils.getDateByStr(etime)) >= 0) {
                System.err.println("=======房价与租金走势========");
                return new Result(ResultCode.ERROR_PARAM);
            }
            record.setCreateTime(DateUtils.getDateByStr(btime));
            record.setUpdateTime(DateUtils.getDateByStr(etime));
        }

        record.setStatus(true);
        List<WebHousePriceRentTrend> list = webHousePriceRentTrendService.selectByBean(record);
        if (list.size() > 0) {
            return new Result(ResultCode.SUCCESS, list);
        } else {
            return new Result(ResultCode.NOT_FOUND);
        }
    }

}
